﻿namespace Hidistro.UI.SaleSystem.Tags
{
    using Hidistro.UI.Common.Controls;
    using System;
    using System.ComponentModel;
    using System.IO;
    using System.Text;
    using System.Web;
    using System.Web.UI.WebControls;

    public class Common_SubmmintOrder_ProductList : AscxTemplatedWebControl
    {
        private Repeater dataListShoppingCrat;
        private Panel pnlShopProductCart;
        public const string TagID = "Common_SubmmintOrder_ProductList";

        public Common_SubmmintOrder_ProductList()
        {
            base.ID = "Common_SubmmintOrder_ProductList";
        }

        protected override void AttachChildControls()
        {
            this.dataListShoppingCrat = (Repeater) this.FindControl("dataListShoppingCrat");
            this.pnlShopProductCart = (Panel) this.FindControl("pnlShopProductCart");
            this.pnlShopProductCart.Visible = false;
        }

        public override void DataBind()
        {
            try
            {
                this.EnsureChildControls();
                if (this.dataListShoppingCrat.DataSource != null)
                {
                    this.dataListShoppingCrat.DataBind();
                }
            }
            catch (Exception e)
            {
                StringBuilder str = new StringBuilder();
                str.AppendLine(DateTime.Now.ToString("yyyy.MM.dd HH:mm:ss"));
                str.AppendLine("客户信息：");
                string ip = "";
                if (HttpContext.Current.Request.ServerVariables.Get("HTTP_X_FORWARDED_FOR") != null)
                {
                    ip = HttpContext.Current.Request.ServerVariables.Get("HTTP_X_FORWARDED_FOR").ToString().Trim();
                }
                else
                {
                    ip = HttpContext.Current.Request.ServerVariables.Get("Remote_Addr").ToString().Trim();
                }
                str.AppendLine("连接IP:" + HttpContext.Current.Request.UserHostAddress);
                str.AppendLine("IP:" + ip);
                str.AppendLine("连接主机名:" + HttpContext.Current.Request.UserHostName);
                str.AppendLine("浏览器:" + HttpContext.Current.Request.Browser.Browser);
                str.AppendLine("浏览器版本:" + HttpContext.Current.Request.Browser.MajorVersion.ToString());
                str.AppendLine("操作系统:" + HttpContext.Current.Request.Browser.Platform);
                str.AppendLine("错误信息:");
                str.AppendLine("页面：" + HttpContext.Current.Request.Url.ToString());
                str.AppendLine("错误信息：" + e.Message);
                str.AppendLine("错误源：" + e.Source);
                str.AppendLine("异常方法：" + e.TargetSite);
                str.AppendLine("堆栈信息：" + e.StackTrace);
                str.AppendLine("--------------------------------------------------------------------------------------------------");
                string upLoadPath = System.Web.Configuration.WebConfigurationManager.AppSettings["LogPath"];
                if (!Directory.Exists(upLoadPath))
                {
                    Directory.CreateDirectory(upLoadPath);
                }
                string FileName;
                string[] flist = Directory.GetFiles(upLoadPath, DateTime.Now.ToString("yyyy.MM.dd") + "_?.log");
                if (flist != null && flist.Length > 0)
                {
                    FileName = upLoadPath + DateTime.Now.ToString("yyyy.MM.dd") + "_" + flist.Length.ToString() + ".log";
                    FileInfo fileInfo = new FileInfo(FileName);
                    if (fileInfo != null && fileInfo.Length >= 100 * 1024 * 1024)
                    {
                        FileName = upLoadPath + DateTime.Now.ToString("yyyy.MM.dd") + "_" + (flist.Length + 1).ToString() + ".log";
                    }
                }
                else
                {
                    FileName = upLoadPath + DateTime.Now.ToString("yyyy.MM.dd") + "_1.log";
                }
                File.AppendAllText(FileName, str.ToString(), Encoding.UTF8);
            }
        }

        protected override void OnInit(EventArgs e)
        {
            if (this.SkinName == null)
            {
                this.SkinName = "/ascx/tags/Common_SubmmintOrder/Skin-Common_SubmmintOrder_ProductList.ascx";
            }
            base.OnInit(e);
        }

        public void ShowProductCart()
        {
            if (this.DataSource == null)
            {
                this.pnlShopProductCart.Visible = false;
            }
            else
            {
                this.pnlShopProductCart.Visible = true;
            }
        }

        [Browsable(false)]
        public object DataSource
        {
            get
            {
                return this.dataListShoppingCrat.DataSource;
            }
            set
            {
                this.EnsureChildControls();
                this.dataListShoppingCrat.DataSource = value;
            }
        }

        public override string ID
        {
            get
            {
                return base.ID;
            }
            set
            {
            }
        }
    }
}

